home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0003_Simulate Star Field.pas < prev    next >
Pascal/Delphi Source File  |  1993-07-16  |  4KB  |  116 lines

  1.  
  2. {Program to simulate travel through a star field - try a different MaxStar}
  3. uses
  4.   TpCrt, TpInline, Graph;    {OpInline used for HiWord only}
  5. const
  6.   MaxStar = 50;                        {num stars}
  7.   MaxHistory = 3;                      {points per streak, = 2**n -1, note mask on line #59}
  8. type
  9.   T_HistoryPoint = record
  10.                      hX, hY : Integer;
  11.                    end;
  12.   T_Star = record
  13.              X, Y       : LongInt;           {star position}
  14.              DX, DY     : LongInt;         {delta}
  15.              DXPositive,
  16.              DYPositive : Boolean;
  17.              Speed      : Word;
  18.              History    : array[0..MaxHistory] of T_HistoryPoint; {Position history}
  19.              HistIndex  : Byte;
  20.            end;
  21.   T_StarArray = array[1..MaxStar] of T_Star;
  22. var
  23.   Gd,
  24.   Gm,
  25.   i,
  26.   j       : Integer;
  27.  
  28.   Color   : Word;
  29.  
  30.   A       : T_StarArray;
  31.   BoundX,
  32.   BoundY,
  33.   CenterX,
  34.  
  35.   CenterY : LongInt;
  36.  
  37.   Angle   : Real;
  38.  
  39.   Shift   : Byte;
  40.  
  41. BEGIN
  42.   Gd := Detect;
  43.   InitGraph(Gd, Gm, '\turbo\tp');
  44.   if GraphResult <> grOk then
  45.     Halt(1);
  46.   Color := GetMaxColor;
  47.   BoundX := GetMaxX * 65536;
  48.   BoundY := GetMaxY * 65536;
  49.   CenterX := GetMaxX * 32768;
  50.   CenterY := GetMaxY * 32768;
  51.   FillChar(A, SizeOf(A), $FF);
  52.   Randomize;
  53.   {Background}
  54.   for i := 1 to 1500 do
  55.     PutPixel(Random(GetMaxX), Random(GetMaxY), Color);
  56.   {Stars}
  57.   repeat
  58.     for i := 1 to MaxStar do
  59.       with A[i] do
  60.         begin
  61.           if (X < 0) or (X > BoundX) or (Y < 0) or (Y > BoundY) then
  62.             begin
  63.             {Position is off-screen, go back to center, new angle}
  64.               Angle := 6.283185 * Random;
  65.               Speed := Random(2000) + 1000;
  66.               DX := Round(Speed * Sin(Angle));
  67.               DY := Round(Speed * Cos(Angle));
  68.               X := 300 * DX + CenterX;
  69.               Y := 300 * DY + CenterY;
  70.               DXPositive := DX > 0;
  71.               DYPositive := DY > 0;
  72.               DX := Abs(DX);
  73.               DY := Abs(DY);
  74.             {Erase all of old line segment}
  75.               for j := 0 to MaxHistory do
  76.                 with History[j] do
  77.                   PutPixel(hX, hY, 0);
  78.             end
  79.           else
  80.             begin               {Plot point}
  81.               Inc(HistIndex);                {Next slot in history}
  82.               HistIndex := HistIndex and $03; { <-- change for new MaxHistory!}
  83.               with History[HistIndex] do
  84.                 begin
  85.                   PutPixel(hX, hY, 0);         {Erase inner dot of line segment}
  86.                   hX := HiWord(X);
  87.                   hY := HiWord(Y);
  88.                   PutPixel(hX, hY, Color);     {New outer dot of line segment}
  89.                 end;
  90.         {Next point}
  91.               if DXPositive then
  92.                 Inc(X, DX)
  93.               else
  94.                 Dec(X, DX); {Add delta}
  95.               if DYPositive then
  96.                 Inc(Y, DY)
  97.               else
  98.                 Dec(Y, DY);
  99.               case Speed of
  100.                 1000..1300 : Shift := 9;
  101.                 1300..1600 : Shift := 8;
  102.                 1600..2100 : Shift := 7;
  103.                 2100..2700 : Shift := 6;
  104.                 2700..2900 : Shift := 5;
  105.                 2900..3000 : Shift := 4;
  106.               end;
  107.               Inc(DX, DX shr Shift);         {Increase delta to accelerate}
  108.               Inc(DY, DY shr Shift);
  109.             end;
  110.         end;
  111.   until KeyPressed;
  112.   ReadLn;
  113.   CloseGraph;
  114. END.
  115.  
  116.